/**
 * org.assess.oa.system.action.TbSystemAction
 */
package org.assess.oa.system.action;

import org.apache.commons.lang.StringUtils;
import org.assess.core.action.BaseAction;
import org.assess.core.service.ServiceResult;
import org.assess.oa.system.bean.TbSystem;
import org.assess.oa.system.service.TbSystemService;

/**
 * @description 系统管理的Action实现类
 * @author XiaLongxiang
 * @createtime 2012-6-6 下午1:56:29
 */
public class TbSystemAction extends BaseAction {
    /** 序列化号 */
    private static final long serialVersionUID = 3919190176128409301L;
    
    /** TbSystem的JavaBean对象 */
    private TbSystem system;
    
    /** 系统管理的Service层对象 */
    private TbSystemService tbSystemService;
    
    /**
     * 跳转到系统管理页面
     * @return
     */
    public String showSystemJsp() {
        return SUCCESS;
    }
    
    /**
     * 查询系统信息，并返回相应的列表
     * @return
     */
    public String listSystem() {
        String jsonString = this.tbSystemService.queryTbSystemAsListString(system);
        super.setJsonString(jsonString);
        return SUCCESS;
    }
    
    /**
     * 加载系统信息
     * @return
     */
    public String loadSystem() {
        if (system != null && StringUtils.isNotBlank(system.getSystem_code())) {
            super.setEdit(true);
            system = this.tbSystemService.queryTbSystemByCode(system);
        }
        else {
            super.setEdit(false);
        }
        return SUCCESS;
    }
    
    /**
     * 保存系统信息
     * @return
     */
    public String saveSystem() {
        try {
            ServiceResult result = this.tbSystemService.saveTbSystem(system, super.isEdit());
            if (result == ServiceResult.save_success) {
                super.setMessage(super.getText("G_SAVE_001", new String[] {super.getText("ST_SYSTEM_000")}));
            }
            else {
                super.setMessage(super.getText("G_SAVE_002", new String[] {super.getText("ST_SYSTEM_000")}));
            }
        }
        catch (Exception ex) {
            super.setMessage(super.getText("G_SAVE_002", new String[] {super.getText("ST_SYSTEM_000")}));
        }
        
        return SUCCESS;
    }
    
    /**
     * 执行保存系统信息前检查，主要是检查系统编码是否重复
     * @return
     */
    public String checkSystem() {
        String jsonString = this.tbSystemService.checkTbSystem(system);
        super.setJsonString(jsonString);
        return SUCCESS;
    }
    
    /**
     * 更改系统信息的valid_flag属性
     * @return
     */
    public String updateValid() {
        if (system == null) {
            system = new TbSystem();
        }
        
        system.setSystem_code(super.getId());
        try {
            String jsonString = this.tbSystemService.updateValid(system);
            super.setJsonString(jsonString);
        }
        catch (Exception ex) {
            
        }
        
        return SUCCESS;
    }
    
    public TbSystem getSystem() {
        return system;
    }
    
    public void setSystem(TbSystem system) {
        this.system = system;
    }
    
    public void setTbSystemService(TbSystemService tbSystemService) {
        this.tbSystemService = tbSystemService;
    }
}
